package uk.co.iceroad.twu.m1;

public class CharacterPrinter {

    private final char characterToPrint;

    public CharacterPrinter(char characterToPrint) {
        this.characterToPrint = characterToPrint;
    }

    public void printCharacter() {
        System.out.print(characterToPrint);
    }

    public void printLine(int numCharsToPrint) {
        for(int i = 0; i< numCharsToPrint; i++)
            printCharacter();
        System.out.println();
    }

    public void printVerticalLine(int linesToPrint) {
        for(int i = 0; i < linesToPrint; i++){
            printCharacter();
            System.out.println();
        }
    }

    public void printRightTriangle(int size) {
        for(int i = 0; i < size; i++)
            printLine(i +1);
    }

    public void printIsoscelesTriangle(int height) {
        for(String line : getIsoscelesTriangle(height))
            System.out.println(line);
    }

    private String[] getIsoscelesTriangle(int height) {

        String[] triangle = new String[height];
        int width = (2* height) -1; //Number of total characters in each line, including blank spaces

        for(int i =0; i< height ; i++){

            int numCharsInLine = (2*i) + 1;
            int numSpaces = (width - numCharsInLine)/2; //Either side of the characters

            String currentLine = getSpacesString(numSpaces);
            for(int j = 0; j < numCharsInLine; j++)
                currentLine += characterToPrint;
            currentLine += getSpacesString(numSpaces);
            triangle[i] = currentLine;
        }
        return triangle;
    }

    private String getSpacesString(int length) {
        String spaces = "";
        for(int j = 0; j < length; j++)
            spaces +=" ";
        return spaces;
    }

    public void printDiamond(int height) {
        String[] triangle = getIsoscelesTriangle(height);
        for(String line : triangle)
            System.out.println(line);

        printDiamondBottom(triangle);
    }

    private void printDiamondBottom(String[] triangle) {
        for (int i = triangle.length -2; i > -1; i--)    //Reprint all but the centre line of the triangle
            System.out.println(triangle[i]);
    }

    public void printDiamondWithName(int height, String name) {
        String[] triangle = getIsoscelesTriangle(height);
        int numSpaces =  (((2* height) -1) - name.length())/2; // Attempt to centre the name, will be off if even number of letters
        triangle[height -1] = getSpacesString(numSpaces) + name;
        for(String line : triangle)
            System.out.println(line);
        printDiamondBottom(triangle);
    }
}
